<!--
 * @Author: 贺楚 2402041262@qq.com
 * @Date: 2023-01-12 15:22:53
 * @LastEditors: 贺楚 2402041262@qq.com
 * @LastEditTime: 2023-01-14 15:57:35
 * @FilePath: \vue-admin-template-master\src\views\permissions\menu\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <el-button style="margin:10px" type="primary" icon="el-icon-refresh-right" circle
      @click="pagePermission"></el-button>
    <el-input placeholder="请输入搜索内容在点击搜索" v-model="searchText" style="width: 300px; background-color: #fff">
      <el-button slot="append" icon="el-icon-search" @click="pagePermission"></el-button>
    </el-input>
    <el-button type="primary" icon="el-icon-plus" style="margin:10px 0" @click="getShow">添加</el-button>
    <!-- 表格 -->
    <el-table style="width: 100%;" border :data="list">
      <el-table-column prop="id" label="id" width="80" align="center">
      </el-table-column>
      <el-table-column prop="pid" label="父权限id" width="80" align="center">
      </el-table-column>
      <el-table-column prop="permName" label="权限名称" width="200" align="center">
      </el-table-column>
      <el-table-column prop="componentName" label="组件名称" align="center" width="200">
      </el-table-column>
      <el-table-column label="权限类型" align="center">
        <template slot-scope="{ row, $index }">
          <p v-if="row.type == 1">菜单</p>
          <p v-else>功能</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <el-tooltip content="修改菜单" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="goUpdate(row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除菜单" placement="top" slot="reference">
            <el-button type="danger" icon="el-icon-delete" @click="deletePermission(row.id)"></el-button>
          </el-tooltip>
          <!-- <el-button type="primary" icon="el-icon-edit" circle></el-button> -->

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination style="margin-top:20px;text-align:center" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="pageNumber" :page-sizes="[10, 50, 100]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 对话框  -->
    <el-dialog :title="state ? '添加权限' : '修改权限'" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmForm" :rules="rules" ref="ruleForm">
        <el-form-item label="父权限id" label-width="100px" prop="pid">
          <el-input autocomplete="off" v-model="tmForm.pid" placeholder="一级菜单请填0，子级菜单请填父菜单id"></el-input>
        </el-form-item>
        <el-form-item label="权限名称" label-width="100px" prop="permName">
          <el-input autocomplete="off" v-model="tmForm.permName"></el-input>
        </el-form-item>
        <el-form-item label="权限类型" label-width="100px" prop="type">
          <el-radio-group v-model="tmForm.type">
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">功能</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="组件名称" label-width="100px">
          <el-input autocomplete="off" v-model="tmForm.componentName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPermission" v-if="state">确 定</el-button>
        <el-button type="primary" @click="updatePermission" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Menu',
  data() {
    return {
      searchText: '',
      dialogFormVisible: false,
      tmForm: {
        pid: '',
        type: '',
        path: '',
        componentName: '',
        permName: ''
      },
      //页码
      pageNumber: 1,
      //每页数量
      pageSize: 10,
      //总条数
      total: 0,
      //列表数据
      list: [],
      // 当前为修改还是添加 修改为false 添加为true
      state: true,
      // 表单验证
      rules: {
        pid: [
          { required: true, message: '请输入父权限id', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请选择权限类型', trigger: 'blur' }
        ],
        permName: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        componentName: [
          { required: true, message: '请输入组件名称', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.pagePermission();
  },
  methods: {
    async addPermission() {
      let res = await this.$API.permissions.addPermission(this.tmForm);
      this.state = true;
      this.dialogFormVisible = false;
      this.pagePermission()
    },
    async pagePermission() {
      let res = await this.$API.permissions.pagePermission({
        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
        searchText: this.searchText

      });
      this.total = res.data.sumLine;
      this.list = res.data.rows;
    },
    // 点击添加
    getShow() {
      this.state = true
      this.dialogFormVisible = true
      this.tmForm = {
        pid: '',
        type: '',
        path: '',
        componentName: '',
        permName: ''
      }
    },
    // 点击修改
    goUpdate(row) {
      this.state = false
      this.dialogFormVisible = true;
      // 浅拷贝  让tmForm收到的数据非服务器直接传回的数据
      this.tmForm = { ...row };
    },
    // 点击修改
    updatePermission() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.permissions.updatePermission(this.tmForm);
          this.dialogFormVisible = false;
          this.pagePermission()
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 删除
    async deletePermission(id) {
      let res = await this.$API.permissions.deletePermission({ id: id });
      this.pagePermission();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.pagePermission();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageNumber = val;
      this.pagePermission();
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>